package com.controller;


import com.DTO.EnrollDTO;
import com.DTO.LoginDTO;
import com.Exception.NormalException.FileExceptions.FileFormatException;
import com.Exception.NormalException.NormalException;
import com.Exception.NormalException.UserExceptions.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.entity.User;
import com.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Api(tags = "用户管理器")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    @ApiResponses({
            @ApiResponse(code = NoSuchUserException.code, message = NoSuchUserException.message),
            @ApiResponse(code = PasswordErrorException.code, message = PasswordErrorException.message)
    })
    public User login(@RequestBody LoginDTO loginDTO, HttpServletResponse response) {
        try {
            return userService.login(loginDTO);
        } catch (NormalException e) {
            e.setStatus(response);
            return null;
        }
    }

    @PostMapping("/enroll")
    @ApiOperation(value = "注册")
    @ApiResponses({
            @ApiResponse(code = PhoneAlreadyEnrollException.code, message = PhoneAlreadyEnrollException.message)
    })
    public boolean enroll(@RequestBody EnrollDTO enrollDTO, HttpServletResponse response) {
        try {
            return this.userService.enroll(enrollDTO);
        } catch (UserException e) {
            e.setStatus(response);
            return false;
        }
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有用户")
    public List<User> getAll() {
        return this.userService.list();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息", notes = "不需要修改的参数不传递\n此接口不可修改手机号、头像")
    public boolean update(@RequestBody User user) {
        System.out.println("user: " + user);
        if(user == null) {
            return true;
        }
        return this.userService.update(user);
    }

    @GetMapping("/updatePhone")
    @ApiOperation(value = "修改用户手机号", notes = "返回修改是否成功")
    @ApiResponses({
            @ApiResponse(code = PhoneAlreadyEnrollException.code, message = PhoneAlreadyEnrollException.message),
            @ApiResponse(code = NullPhoneException.code, message = NullPhoneException.message)
    })
    public boolean updatePhone(Integer userId, String phone, HttpServletResponse response){
        try {
            return this.userService.updatePhone(userId, phone);
        } catch (NormalException e) {
            e.setStatus(response);
            return false;
        }
    }

    @GetMapping("/getByPhone")
    @ApiOperation(value = "根据手机号精确查找用户")
    public User getByPhone(String phone) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return this.userService.getOne(wrapper);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据id精确查找用户")
    public User getById(Integer userId) {
        return this.userService.getById(userId);
    }

    @GetMapping("/getByPhoneOrId")
    @ApiOperation(value = "根据手机号或id精确查找用户")
    public List<User> getByPhoneOrId(String num) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        String phone = num;
        Integer id = null;
        try {
            id = Integer.valueOf(num);
            wrapper.eq("phone", phone)
                    .or()
                    .eq("id", id);
        } catch (NumberFormatException e) {
            wrapper.eq("phone", phone);
        }
        return this.userService.list(wrapper);
    }

    @GetMapping("/getByName")
    @ApiOperation(value = "根据用户名模糊查找用户")
    public List<User> getByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return this.userService.list(wrapper);
    }

    @PostMapping("/uploadHead")
    @ApiOperation(value = "上传头像", notes = "返回头像url")
    @ApiResponses({
            @ApiResponse(code = FileFormatException.code, message = FileFormatException.message)
    })
    public String uploadHead(Integer userId, MultipartFile head, HttpServletResponse response) {
        try {
            return this.userService.uploadHead(userId, head);
        } catch (FileFormatException e) {
            e.setStatus(response);
            return null;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

